package com.wcity.manage.web.converter;

import com.wcity.manage.common.utils.DateUtil;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;

/**
 * spring mvc自定义日期转换
 * @author sxd
 */
@Component
public class DateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String source){
        try{
            if(source == null){
                return null;
            }else if(source.length() == 10){//yyyy:MM:dd
                return DateUtil.parse(source, DateUtil.PAT_DATE);
            }else if(source.length() == 19){//yyyy:MM:dd HH:mm:ss
                return DateUtil.parse(source, DateUtil.PAT_DATE_TIME);
            }else{
                return null;
            }
        }catch (ParseException e){
            return null;
        }
    }
}
